package com.wskh.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wskh.commonUtil.JwtUtils;
import com.wskh.commonUtil.R;
import com.wskh.edu.entity.Course;
import com.wskh.edu.entity.TOrder;
import com.wskh.edu.entity.Teacher;
import com.wskh.edu.entity.chapter.ChapterVo;
import com.wskh.edu.entity.frontvo.CourseFrontVo;
import com.wskh.edu.entity.subject.OneSubject;
import com.wskh.edu.entity.vo.front.CourseWebVo;
import com.wskh.edu.mapper.CourseMapper;
import com.wskh.edu.service.ChapterService;
import com.wskh.edu.service.CourseService;
import com.wskh.edu.service.SubjectService;
import com.wskh.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(value="课程前台展示")
@RestController
@RequestMapping("/eduService/courseFront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    TeacherService teacherService;
    @Autowired
    CourseService courseService;
    @Autowired
    SubjectService subjectService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    CourseMapper courseMapper;

    @PostMapping(value = "getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseFrontVo", value = "查询对象", required = false)
            @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<Course> pageParam = new Page<Course>(page, limit);
        Map<String, Object> map = courseService.getFrontCourseList(pageParam, courseFrontVo);
        return R.ok().data(map);
    }

    @GetMapping(value = "getAllSubject")
    public R getAllSubject(){
        List<OneSubject> allSubject = subjectService.getAllSubject();
        return R.ok().data("items",allSubject);
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "getCourseById/{courseId}")
    public R getCourseById(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId, HttpServletRequest request){
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
        // 根据当前登录用户id和课程id判断该用户是否购买该课程
        boolean isBuy = false;
        if(courseWebVo.getPrice().equals(new BigDecimal(0))){
            isBuy = true;
        }else{
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            if(StringUtils.isEmpty(memberId)){
                isBuy = false;
            }else{
                TOrder tOrderByMemberIdAndCourseId = courseMapper.getTOrderByMemberIdAndCourseId(memberId, courseId);
                isBuy = (tOrderByMemberIdAndCourseId!=null&&tOrderByMemberIdAndCourseId.getPayType()==1);
            }
        }
        return R.ok().data("course", courseWebVo).data("chapterVoList",
                chapterVoList).data("isBuy",isBuy);

    }

    //根据课程id查询课程信息
    @GetMapping("getCourseInfoDto/{courseId}")
    public CourseWebVo getCourseInfoDto(@PathVariable String courseId) {

        CourseWebVo courseInfoForm = courseService.selectInfoWebById(courseId);
        CourseWebVo courseInfo = new
                CourseWebVo();
        BeanUtils.copyProperties(courseInfoForm,courseInfo);

        return courseInfo;

    }

}
